package com.site.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/** IOUtil工具类
 * @author xiaochi
 * @date 2022/6/24 10:52
 * @desc IOUtil
 */
public class IOUtil {

    /**
     * 从文件到文件
     * @param in
     * @param out
     * @return 复制的字节数
     */
    public static int copy(File in, File out) throws IOException {
        if (in == null || out == null){
            return 0;
        }
        InputStream inputStream = new FileInputStream(in);
        FileOutputStream outputStream = new FileOutputStream(out);
        BufferedInputStream bin = new BufferedInputStream(inputStream);
        BufferedOutputStream bout = new BufferedOutputStream(outputStream);
        try {
            return copy(bin, bout);
        }finally {
            bin.close();
            bout.close();
            inputStream.close();
            outputStream.close();
        }
    }

    /**
     * 从输入流到输出流
     * @param in
     * @param out
     * @return 复制的字节数
     */
    public static int copy(InputStream in, OutputStream out) throws IOException {
        if (in == null || out == null){
            return 0;
        }
        /*byte[] bytes = new byte[in.available()];
        int byteCount = in.read(bytes);
        in.close();
        out.write(bytes);
        out.flush();*/
        int byteCount = 0;
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1){
            out.write(buffer,0,bytesRead);
            byteCount += bytesRead;
        }
        out.flush();
        return byteCount;
    }

    /**
     * 从文件读入到字节数组中
     * @param in
     * @return
     * @throws IOException
     */
    public static byte[] copyToByteArray(File in) throws IOException {
        if (in == null){
            return new byte[0];
        }
        return copyToByteArray(Files.newInputStream(in.toPath()));
    }

    /**
     * 从输入流中读入到字节数组中
     * @param in
     * @return
     */
    public static byte[] copyToByteArray(InputStream in) throws IOException {
        if (in == null){
            return new byte[0];
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            copy(in,out);
            return out.toByteArray();
        } finally {
            out.close();
        }
    }

    /**
     * 从文件中读入到字符串中
     * @param in
     * @return
     */
    public static String copyToString(File in) throws IOException {
        if (in == null){
            return "";
        }
        return copyToString(Files.newInputStream(in.toPath()));
    }

    /**
     * 从输入流中读入到字符串中
     * @param in
     * @return
     */
    public static String copyToString(InputStream in) throws IOException {
        if (in == null){
            return "";
        }
        return new String(copyToByteArray(in), StandardCharsets.UTF_8);
    }
}
